package com.baize.minespace.controller;


import com.baize.minespace.pojo.Posts;
import com.baize.minespace.service.PostsService;
import com.baize.minespace.utils.Result;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
/**
 * <p>
 * 博客文章元数据表 前端控制器
 * </p>
 *
 * @author baize
 * @since 2024-04-15
 */
@RestController
@RequestMapping("/posts")
public class PostsController {
    @Autowired
    private PostsService postsService;

    @ApiModelProperty("新增博客文章元数据表")
    @ApiOperation(value = "新增博客文章元数据表", notes = "新增博客文章元数据表")
    @PostMapping("add")
    public Result<Boolean> addPosts(@RequestBody Posts posts){
        return Result.success(postsService.save(posts));
    }

    @ApiModelProperty("删除博客文章元数据表")
    @ApiOperation(value = "删除博客文章元数据表", notes = "删除博客文章元数据表")
    @GetMapping ("delete/{id}")
    public Result<Boolean> deletePosts(@PathVariable(value = "id") Integer id) {
        return Result.success(postsService.removeById(id));
    }

    @ApiModelProperty("修改博客文章元数据表")
    @ApiOperation(value = "修改博客文章元数据表", notes = "修改博客文章元数据表")
    @PostMapping("update")
    public Result<Boolean> updatePosts(@RequestBody Posts posts) {
        return Result.success(postsService.updateById(posts));
    }

    @ApiModelProperty("查询博客文章元数据表")
    @GetMapping("select/{id}")
    public Result<Posts> selectPosts(@PathVariable(value = "id") Integer id) {
        return Result.success(postsService.getById(id));
    }

    @ApiModelProperty("分页查询博客文章元数据表")
    @ApiOperation(value = "分页查询博客文章元数据表", notes = "分页查询博客文章元数据表")
    @GetMapping("page/{rows}/{page}")
    public Result<IPage<Posts>> selectPostsPage(
        @PathVariable(value = "rows") Integer rows,
        @PathVariable(value = "page") Integer page,
        @RequestBody Posts posts
    ) {
        return postsService.selectPostsPage(rows, page, posts);
    }
}
